﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using DWF.Studio.Custom;
using DWF.Studio.ISerivces;
using DWF.Studio.Project;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DWF.Studio.Services
{
    class ActivityNoticeService : IActivityNoticeService
    {
        public void ActivityNotice(string msg, string data)
        {
            if (msg == StaticResource.NOTICE_REFRESH_IMAGE)
            {
                var projectManager = ProjectManager.Instance;
                if (projectManager != null)
                {
                    projectManager.RefreshImageFolder();
                }
            }
            else if (msg == StaticResource.NOTICE_REFRESH_RES)
            {
                var projectManager = ProjectManager.Instance;
                if (projectManager != null)
                {
                    projectManager.RefreshResFolder();
                }
            }

            else if (msg == StaticResource.NOTICE_MAINWIN_MIX)
            {
                var mainWindow = App.Current.MainWindow;
                mainWindow.WindowState = System.Windows.WindowState.Minimized;
            }
            else if (msg == StaticResource.NOTICE_MAINWIN_MAX)
            {
                var mainWindow = App.Current.MainWindow;
                mainWindow.WindowState = System.Windows.WindowState.Maximized;
            }
            else if (msg == StaticResource.NOTICE_MAINWIN_ACTIVATE)
            {
                var mainWindow = App.Current.MainWindow;
                mainWindow.WindowState = System.Windows.WindowState.Normal;
                mainWindow.Activate();
            }
            else if (msg == StaticResource.NOTICE_OPEN_SCRIPT)
            {
                if (!String.IsNullOrWhiteSpace(data))
                {
                    var fileName = data;
                    var ext = System.IO.Path.GetExtension(fileName);
                    var projectManager = ProjectManager.Instance;
                    if (projectManager != null && projectManager.CurrentProject != null)
                    {
                        var projectInfo = projectManager.CurrentProject;
                        var fullName = System.IO.Path.Combine(projectInfo.FullName, StaticResource.ProjectSelfFile, fileName);
                        var projectFile = projectManager.GetProjectFile(fullName);
                        if (projectFile != null)
                        {
                            GlobalActionHandler.ScriptShow?.Invoke(new TagEventArgs<Project.ProjectFile>(projectFile));
                        }
                        else
                        {
                            var forder = projectInfo.Children.FirstOrDefault(item => item.NodeType == EProjectNodeType.Folder && item.Name == StaticResource.ProjectSelfFile);

                            ProjectFile file = null;
                            if (ext == StaticResource.XamlExtension)
                            {
                                if (projectManager.CurrentProject.WFLanguage == StaticResource.WorkflowLanguage_vb)
                                {
                                    file = projectManager.CreateXamlFileToProject(forder, fileName, StaticResource.XamlFileTemplateSequence_vb);
                                }
                                else if (projectManager.CurrentProject.WFLanguage == StaticResource.WorkflowLanguage_cs)
                                {
                                    file = projectManager.CreateXamlFileToProject(forder, fileName, StaticResource.XamlFileTemplateSequence_cs);
                                }
                            }

                            if (file != null)
                            {
                                GlobalActionHandler.ScriptShow?.Invoke(new TagEventArgs<Project.ProjectFile>(file));
                            }
                        }
                    }
                }
            }
        }

    }
}
